package com.toommi.refuse.controller.admin;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.SysRole;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.model.RolePermissionModel;
import com.toommi.refuse.service.admin.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * ..
 * 角色管理
 *
 * @author yangg
 * @since 2019-07-29 17:48
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/admin")
@PermissionController(groupCode = "sys_role", groupName = "角色管理")
@Api(tags = "角色、权限")
public class   SysRoleController {

    private SysRoleService sysRoleService;

    /**
     * 新增角色
     *
     * @param sysRole 系统角色
     * @return 操作结果
     */
    @ApiOperation(value = "新增角色")
    @ApiImplicitParam(name = "sysRole", value = "角色详细实体", required = true, dataType = "SysRole")
    @RequiresPermissions("admin:role:add")
    @PermissionReport(value = "admin:role:add", name = "新增角色", group = "sys_role")
    @PostMapping(value = "/role", produces = "application/json;charset=UTF-8")
    public CommonResponse addSysRole(@RequestBody SysRole sysRole) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysRoleService.addSysRole(sysRole);
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add admin role error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除指定的角色
     *
     * @param id 角色id
     * @return 操作结果
     */
    @ApiOperation(value="删除指定角色", notes="根据url的id来删除角指定角色")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long")
    @RequiresPermissions("admin:role:delete")
    @PermissionReport(value = "admin:role:delete", name = "删除角色", group = "sys_role")
    @DeleteMapping(value = "/role/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse deleteRole(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysRoleService.deleteSysRole(id);
        } catch (Exception e) {
            log.error("delete admin role error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 查询角色列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param keyWord  查询关键字
     * @return 角色列表
     */
    @ApiOperation(value="查询角色列表", notes="获取系统角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "keyWord", value = "关键字", required = false, dataType = "String")
    })
    @GetMapping(value = "/roles", produces = "application/json;charset=UTF-8")
    public CommonResponse findRole(int pageNum, int pageSize, String keyWord) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            commonResponse.setData(sysRoleService.findSysRoles(pageNum, pageSize, keyWord));
        } catch (Exception e) {
            log.error("get admin roles error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 新增/修改角色权限信息
     *
     * @param rolePermissionModel 角色权限信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增角色权限")
    @ApiImplicitParam(name = "rolePermissionModel", value = "角色权限详细实体", required = true, dataType = "RolePermissionModel")
    @RequiresPermissions("admin:role:permission:add")
    @PermissionReport(value = "admin:role:permission:add", group = "sys_role", name = "编辑角色权限")
    @PostMapping(value = "/role/permissions", produces = "application/json;charset=UTF-8")
    public CommonResponse addRolePermission(@RequestBody RolePermissionModel rolePermissionModel) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysRoleService.addRolePermission(rolePermissionModel);
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add admin role permission error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 查询指定角色拥有的权限集合
     *
     * @param id 角色id
     * @return 角色拥有的权限集合
     */
    @ApiOperation(value="查询指定角色拥有的权限集合", notes="根据url的id来查询指定角色拥有的权限集合")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long")
    @GetMapping(value = "/role/{id}/permissions", produces = "application/json;charset=UTF-8")
    public CommonResponse getRolePermissions(@PathVariable(name = "id") long id) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(sysRoleService.findRolePermissions(id));
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("get admin role permission error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
}
